package model.abstracts;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

import changelog.Changelog;
import changelog.ChangelogResource;
import load.Init;
import view.Page;
import model.*;
import tools.Tools;

public class Main {

    public static final List<Writable> WRITABLES = new ArrayList<>();
    public static final Map<String, Writable> WRITABLES_ZHINDEX = new HashMap<>();

    public static void load () {
        Era.load();
        Civilization.load();
        Leader.load();
        Trait.load();
        Unit.load();
        District.load();
        Building.load(); 
        Improvement.load();
        CityState.load();
        YieldChange.load();
        Technology.load();
        Civic.load();
        Wonder.load();
        UnitPromotion.load();
        UnitPromotionClass.load();
        GreatPerson.load();
        Government.load();
        Policy.load();
        Commemoration.load();
        Belief.load();
        Terrain.load();
        Feature.load();
        NaturalWonder.load();
		ResourceCategory.load();
        Resource.load();
        Governor.load();
        GovernorPromotion.load();
        Project.load();

        Trait.linkData();
        District.linkData();
        Unit.linkData();
    }

    public static void write (String language) {
        for (Writable writable : WRITABLES) {
            writable.writeJSON(language);
        }
    }

    public static void writeAll () {
        for (String language : Page.LANGUAGES) {
            write(language);
        }
    }

    public static void buildChangelog (String version, String output) throws Exception {
        System.out.println("loading changelog txt");
        Changelog changelog = new Changelog(version, version + "\\S*");
        File logs = new File("C:/Users/xiaoxiao/Documents/My Games/Sid Meier's Civilization VI/Mods/civ6-harmony-in-diversity/Changelog");
        for (File f : logs.listFiles()) {
            if (!f.getName().endsWith(".txt") || f.getName().equals("changelog_old.txt")) continue;
            changelog.readFrom(f, "rel");
        }
        System.out.println("converting into json");
        JSONObject object = changelog.toJsonObject(output);
        System.out.println("writing json");
        File file = new File("manual/json/zh_Hans_CN/concepts/updates/" + output + ".json");
        if (file.exists()) {
            file.delete();
        }
        file.createNewFile();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
        writer.write(object.toString());
        writer.flush();
        writer.close();
    }

    public static void main(String[] args) throws Exception {
        if (args.length > 0) {
            String cmd = args[0];
            if (cmd.equals("page")) {
                System.out.println("converting json into html...");
                Page.convertAll();
            } else if (cmd.equals("init")) {
                Init.main(null);
            } else if (cmd.equals("icons")) {
                Init.initIcons();
            } else if (cmd.equals("changelog")) {
                System.out.println("loading...");
                load();

                Page.deleteFiles(new File("json"));
                System.out.println("writing json...");
                writeAll();
                
                System.out.println("saving changelog resource...");
                ChangelogResource.saveResourceFile();
            } else if (cmd.equals("build")) {
                buildChangelog(args[1], args.length >= 3 ? args[2] : "v" + args[1]);
            } else {
                System.out.println("unknown command");
            }
        } else {
            System.out.println("loading...");
            load();
            
            Page.deleteFiles(new File("json"));
            System.out.println("writing json...");
            writeAll();
    
            System.out.println("converting json into html...");
            Page.convertAll();
    
            System.out.println("writing tag excel...");
            Tools.writeExcel();
    
            System.out.println("saving changelog resource...");
            ChangelogResource.saveResourceFile();
        }
    }
}
    